<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Race extends Controller
{
    
    public function index()
    {
        $year = date('Y');
        $month = input()['month']?:date('m',time()-86400*5 );
        $startTime = strtotime($year.'-'.$month.'-01');
        $endTime = strtotime(date('Y-m-t',$startTime) )+86399;
        if(input()['page'] )
        {
            $adminId = session('uid','','admin');
            $list = db('race_list')
                ->where('is_del',0)
                //->where('end_date','>=',$startTime)
                //->where('end_date','<=',$endTime)
                ;
            $list = $list->order('id DESC')->select();
            foreach ($list as $k => $v)
            {
                $list[$k]['start_date'] = date('Y-m-d',$v['start_date']);
                $list[$k]['end_date'] = date('Y-m-d',$v['end_date']);
                $list[$k]['race_type'] = $v['race_type'] == 1?'个人':'部门';
                $list[$k]['race_system'] = $v['race_system'] == 1?'能':'不能';
                $list[$k]['race_system_min_value'] = $v['race_system'] == 1?$v['race_system_min_value']:'';
            }
            $count = db('race_list')->count();
            return ['code'=>0,'msg'=>'','count'=>500,'data'=>$list]; 
        }
        $this->assign([
            'month'=>$month,
            'year'=>$year,
        ]);
        return view();
    }
    
    public function edit()
    {

        if(input('post.'))
        {
            $data = input('post.');
            $data['start_date'] = strtotime($data['start_date'] );
            $data['end_date'] = strtotime($data['end_date'] );
            if(input('id'))
            {
                $data['race_system_min_value'] = $data['race_system'] == 1?$data['race_system_min_value']:0;
                $sqlStatus = db('race_list')->where('id',input('id'))->update($data);
            }
            else
            {
                $sqlStatus = db('race_list')->insert($data);
            }
            return $sqlStatus?['msg'=>'操作成功','icon'=>1,'code'=>1]:['msg'=>'操作失败','icon'=>2];
        }
        else 
        {
            if(input('id'))
            {
                $data = db('race_list')->where('id',input('id'))->find();
                $data['start_date'] = date('Y-m-d',$data['start_date']);
                $data['end_date'] = date('Y-m-d',$data['end_date']);
            }
            
            $this->assign([
                'data'=>$data,
            ]);
            return view();
        }
        
        
    }
    
    
    public function race_detail()
    {
        if(input('race_id') )
        {
            $raceInfo = db('race_list')->where('id',input('race_id'))->find();
            $raceInfo['start_date'] = date('Y-m-d',$raceInfo['start_date']);
            $raceInfo['end_date'] = date('Y-m-d',$raceInfo['end_date']);
            $adminId = session('uid','','admin');
            $groupId = session('group_id','','admin');

            // 个人PK
            if($raceInfo['race_type'] == 1)
            {
                /*if(session('group_id','','admin') == 2)
                {
                    
                }*/
                for ($i = 1; $i < 5; $i++) {
                    
                    $departName[$i] = db('admin')->where('department_id',$i)->where('status',1)->where('group_id',5)->value('group_name');
                    $departList[$i] = db('admin')->where('department_id',$i)->where('status',1)->where('id','<>',$adminId)->where('group_id',2)->select();
                    foreach ($departList[$i] as $k=>$v )
                    {
                        // 和自己的PK关系
                        if($groupId == 2)
                        {
                            $departList[$i][$k]['race_info'] = db('race_staff_data')->where('race_id',$raceInfo['id'])->where('admin_id',$adminId)->where('to_admin_id',$v['id'])->order('id DESC')->find();
                            $departList[$i][$k]['to_race_info'] = db('race_staff_data')->where('race_id',$raceInfo['id'])->where('to_admin_id',$adminId)->where('admin_id',$v['id'])->order('id DESC')->find();
                        }
                        // 全部PK关系
                        $departList[$i][$k]['race_list'] = db('race_staff_data')->where('race_id',$raceInfo['id'])->where("to_admin_id = {$v['id']} OR admin_id = {$v['id']}")->select();
                    }
                }
                $raceList = db('race_staff_data')->where('race_id',$raceInfo['id'])->where('status',1)->order('id DESC')->select();
                $this->assign([
                    'raceInfo' => $raceInfo,
                    'raceList' => $raceList,
                    'departList'=>$departList,
                    'departName'=>$departName,
                ]);
                return view();
            }
            else 
            {
                
                
                for ($i = 1; $i < 5; $i++) {
                    $departList[$i] = db('admin')->where('department_id',input('id'))->where('status',1)->where('group_id',5)->select();
                    foreach ($departList[$i] as $k=>$v )
                    {
                        // 和自己的PK关系
                        if($groupId == 5)
                        {
                            $departList[$i][$k]['race_info'] = db('race_department_data')->where('race_id',$raceInfo['id'])->where('department_id',$i)->where('to_department_id',$v['department_id'])->find();
                            $departList[$i][$k]['to_race_info'] = db('race_department_data')->where('race_id',$raceInfo['id'])->where('to_department_id',$i)->where('department_id',$v['department_id'])->find();
                        }
                        // 全部PK关系
                        $departList[$i][$k]['race_list'] = db('race_department_data')->where('race_id',$raceInfo['id'])->where("to_department_id = $i OR department_id = $i")->select();
                    }
                }
                print_r($departList);
            }
        }
    }
    
    
    public function race_staff_add()
    {
        
        if(input('post.'))
        {
            //if(session('group_id','','admin') != 5) return;
            $data = input('post.');
            
            $data['admin_id'] = session('uid','','admin');
            if(!$data['to_admin_id'] || !$data['admin_id'])
                return ['msg'=>'操作失败','icon'=>2];
            
            $data['status'] = 0;
            $progress = [
                0=>['text'=>idToAdminname($data['admin_id']).'发起了PK','time'=>time()] , 
                1=>['text'=>'PK内容为：'.str_replace(PHP_EOL,'<br>',$data['progress_content']),'time'=>time() ] 
            ];
            $data['progress'] = json_encode($progress);
            $sqlStatus = db('race_staff_data')->insert($data);
            return $sqlStatus?['msg'=>'操作成功','icon'=>1,'code'=>1]:['msg'=>'操作失败','icon'=>2];
        }
        else 
        {
            if(input('race_id'))
            {
                $raceInfo = db('race_list')->where('id',input('race_id'))->find();
                //$data['start_date'] = date('Y-m-d',$data['start_date']);
                //$data['end_date'] = date('Y-m-d',$data['end_date']);
            }
            
            $this->assign([
                'raceInfo'=>$raceInfo,
            ]);
            return view();
        }
        
    }
    
    public function race_staff_detail()
    {
        if(input('id'))
        {
            $data = db('race_staff_data')->where('id',input('id'))->find();
            $progress = json_decode($data['progress'],true);
            $raceInfo = db('race_list')->where('id',$data['race_id'])->find();
            $raceInfo['start_date'] = date('Y-m-d',$data['start_date']);
            $raceInfo['end_date'] = date('Y-m-d',$data['end_date']);
            $this->assign([
                'raceInfo'=>$raceInfo,
                'data'=>$data,
                'progress'=>$progress,
            ]);
            return view();
        }
    }
}
    
?>